// ==UserScript==
// @name         复制
// @namespace    http://tampermonkey.net/
// @version      2024-04-01
// @description  try to take over the world!
// @author       You
// @match        *://se.mhtall.com/cuggw/rs/*
// @icon         https://www.google.com/s2/favicons?sz=64&domain=mhtall.com
// @grant        none
// ==/UserScript==

(function () {
    /**
     * 动态加载js
     * @param src
     * @param name
     */
    function importScript(src, name) {
        let script = document.createElement("script");
        script.src = src;
        script.onload = function () {
            console.log(name + "加载成功")
            msg(name + "加载成功", 'green', 900)
        }
        document.body.appendChild(script);
    }

    /**
     * 判断字符串是否包含子串
     * @param str 源字符串
     * @param substr 子串
     * @returns {boolean}
     */
    function contain(str, substr) {
        return str.indexOf(substr) !== -1;
    }

    /**
     * 弹出消息
     * @param msg 消息
     * @param type 类型
     * @param time
     */
    function msg(msg, type, time) {
        let htmlDivElement = document.createElement("div-my-msg");
        htmlDivElement.innerHTML = msg;
        htmlDivElement.style.position = "fixed";
        htmlDivElement.style.font = "bold 20px Arial";
        htmlDivElement.style.left = "50%";
        htmlDivElement.style.top = "20%";
        htmlDivElement.style.zIndex = "9999";
        htmlDivElement.style.color = type || 'red';
        time = time || 3000;
        if (document.getElementsByTagName("div-my-msg").length > 1) {
            setTimeout(
                function () {
                    document.getElementsByTagName("div-my-msg")[0].remove();
                }, 100
            )
        }
        document.body.appendChild(htmlDivElement);
        setTimeout(function () {
            htmlDivElement.remove();
        }, time);
    }


    importScript('https://unpkg.com/axios/dist/axios.min.js', 'axios')


    function parseAnswer(data) {
        // 正则匹配
        let reg = /<span class=\"div_answer\">(.*?)<\/span>/g;
        let answer = [];
        try {
            let arr = data.match(reg);
            for (let i = 0; i < arr.length; i++) {
                answer[i] = arr[i].replace(/<span class=\"div_answer\">/g, '').replace(/<\/span>/g, '').replace(/&nbsp;/g, '')
                answer[i] = answer[i].replace(/[A-Z]、/g, '')
            }
            if (answer.length > 1 || answer.length === 0) {
                msg("答案数量错误", 'red', 2000)
            }
            console.log("解析成功 ： " + answer)
        } catch (e) {
            console.log(e)
        }
        return answer
    }


    async function doSearch(keyword) {
        let arr = []
        const res = await axios.get("https://test.javademo.top/mht/rest/course/exercise/item/search/10792?keywords=" + keyword).then(function (res) {
            let html = res.data;
            arr = parseAnswer(html)
            console.log("搜索完成 ： ", arr)
        })
        return arr
    }


    function getItem() {
        let items = document.getElementsByClassName("item_li");

        for (let i = 0; i < items.length; i++) {
            let item = items[i];
            let buttonElement = document.createElement("button");
            buttonElement.innerText = '查询';
            item.appendChild(buttonElement)

            let itemTitle = item.getElementsByClassName("item_title")[0].innerText;
            itemTitle = itemTitle.replace(/^\d+、/, "");
            let itemAnswers = item.getElementsByClassName("item_li_option_ul_li");

            buttonElement.onclick = async function () {
                const anwser = await doSearch(itemTitle)
                // navigator.clipboard.writeText(itemTitle);
                msg("复制成功", 'green', 900)
                setTimeout(() => {
                    for (let j = 0; j < itemAnswers.length; j++) {
                        if (contain(itemAnswers[j].innerText, anwser[0])) {
                            console.log("找到答案")
                            itemAnswers[j].getElementsByTagName("INPUT")[0].click()
                        }
                    }
                }, 100)

            }
        }
    }

    getItem()


})();
